package com.zy.edu.service.impl;

import com.zy.edu.bean.CourseChapterExercisesStu;
import com.zy.edu.dao.CourseChapterExercisesStuDao;
import com.zy.edu.service.CourseChapterExercisesStuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-07-28
 */
@Service
public class CourseChapterExercisesStuServiceImpl extends ServiceImpl<CourseChapterExercisesStuDao, CourseChapterExercisesStu> implements CourseChapterExercisesStuService {

    @Override
    public String getExerciseRate(String id) {
        DecimalFormat decimalFormat= new  DecimalFormat( "0.00" );

        long total = this.baseMapper.countAllByUserId(id);
        long correct = this.baseMapper.countCorrectByUserId(id);
        if(total==0) {
            return "0";
        }
        float res = Float.valueOf(correct)/total;
        String format = decimalFormat.format(res);
        return format;
    }
}
